<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item :label="$t('workOrder.title')" prop="title">
        <el-input
          v-model="queryParams.title"
          :placeholder="$t('workOrder.titlePlacehold')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item :label="$t('workOrder.orderStatus')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('workOrder.statusPlaceholder')" clearable>
          <el-option
            v-for="dict in dict.type.workorder_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item :label="$t('workOrder.AcceptanceTime')" prop="acceptedTime">
        <el-date-picker clearable
          v-model="queryParams.acceptedTime"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('workOrder.AcceptanceTimePlaceHolder')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('workOrder.finishTime')" prop="handleFinishTime">
        <el-date-picker clearable
          v-model="queryParams.handleFinishTime"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('workOrder.finishTimePlaceHolder')">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5"> -->
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ems:emsWorkorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ems:emsWorkorder:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ems:emsWorkorder:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emsWorkorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('workOrder.table.title')" align="center" prop="title" />
      <el-table-column :label="$t('workOrder.table.deviceName')" align="center" prop="deviceName" />
      <el-table-column :label="$t('workOrder.table.deviceAddress')" align="center" prop="deviceAddress" />
      <el-table-column :label="$t('workOrder.table.RelatedAlarm')" align="center" prop="eventName" />
      <el-table-column :label="$t('workOrder.table.image')" align="center" prop="image1" >
        <template slot-scope="scope">
          <el-image :src="scope.row.image1" v-if="scope.row.image1" style="width:80px;height:80px;" :preview-src-list="[scope.row.image1,scope.row.image2,scope.row.image3]"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('workOrder.table.status')" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.workorder_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('workOrder.table.acceptanceUser')" align="center" prop="acceptedUser" />
      <el-table-column :label="$t('workOrder.table.AcceptanceTime')" align="center" prop="acceptedTime" width="180" />
      <el-table-column :label="$t('workOrder.table.handleOption')" align="center" prop="handleOption" />
      <el-table-column :label="$t('workOrder.table.finishTime')" align="center" prop="closedTime" width="180" />
      <el-table-column :label="$t('workOrder.table.operator')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-dot-square"
            v-if="scope.row.status === 'Allocation' || scope.row.status === 'Handling'"
            @click="handleUpdate(scope.row , 'HandleFinish')"
            v-hasPermi="['ems:emsWorkorder:edit']"
          >{{$t('workOrder.table.handle')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            v-if="scope.row.status === 'HandleFinish'"
            @click="handleClosed(scope.row , 'Closed')"
            v-hasPermi="['ems:emsWorkorder:edit']"
          >{{$t('workOrder.table.closed')}}</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ems:emsWorkorder:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单对话框 -->
    <el-dialog :title="$t('workOrder.form.name')" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item :label="$t('workOrder.form.apply')" prop="applyUser">
          <el-input v-model="form.applyUser" disabled/>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.phoneNumber')" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" disabled/>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.orderTitle')" prop="title">
          <el-input v-model="form.title"  disabled/>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.deviceName')" prop="deviceId">
          <el-input v-model="form.deviceName"  disabled/>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.RelatedAlarm')" prop="eventDataId">
          <el-input v-model="form.eventName" disabled/>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.image')" prop="image1">
          <div class="demo-image__placeholder">
          <el-image :src="form.image1" style="width:100px;height:100px;" v-if="form.image1" :preview-src-list="[form.image1]" />
          <el-image :src="form.image2" style="width:100px;height:100px;margin-left:10px" v-if="form.image2" :preview-src-list="[form.image1]" />
          <el-image :src="form.image3" style="width:100px;height:100px;margin-left:10px" v-if="form.image3" :preview-src-list="[form.image1]" />
          </div>
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.description')" prop="description">
          <el-input v-model="form.description" type="textarea" disabled />
        </el-form-item>
        <el-form-item :label="$t('workOrder.form.handleOption')" prop="handleOption">
          <el-input v-model="form.handleOption" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandle">{{$t('common.confirm')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmsWorkorder, getEmsWorkorder, delEmsWorkorder, addEmsWorkorder, updateEmsWorkorder ,handleOrder , handleDetail } from "@/api/ems/emsWorkorder";
import { workOrderDevopsOptions } from "@/api/ems/workorderDevops";
export default {
  name: "EmsWorkorder",
  dicts: ['workorder_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单表格数据
      emsWorkorderList: [],
      devopsUserList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      allocationVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        deviceId: null,
        eventDataId: null,
        image1: null,
        image2: null,
        image3: null,
        status: null,
        acceptedBy: null,
        acceptedTime: null,
        handleOption: null,
        handleFinishTime: null,
        description: null,
        score: null,
        evaluate: null,
        evaluateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "工单标题不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备Id不能为空", trigger: "blur" }
        ],
        eventDataId: [
          { required: true, message: "关联告警数据Id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工单列表 */
    getList() {
      this.loading = true;
      listEmsWorkorder(this.queryParams).then(response => {
        this.emsWorkorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWorkOrderDevopsOptions(){
      workOrderDevopsOptions().then( res => this.devopsUserList = res.data)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.allocationVisible = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        deviceId: null,
        eventDataId: null,
        image1: null,
        image2: null,
        image3: null,
        status: null,
        acceptedBy: null,
        acceptedTime: null,
        handleOption: null,
        handleFinishTime: null,
        description: null,
        score: null,
        evaluate: null,
        evaluateTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //显示分配受理人窗口
    showAllocation(row){
      getEmsWorkorder(row.id).then(response => {
        this.form = response.data;
        this.allocationVisible = true;
        this.title = "分配受理人";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      handleDetail(row).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "处理工单";
      });
    },
    handleClosed(row){
      this.$modal.confirm('是否确认关闭工单?').then(function() {
        let param = {id:row.id , action:'Closed'}
        return handleOrder(param);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    submitHandle(){
      this.form.action = 'HandleFinish';
      this.submitForm()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          handleOrder(this.form).then(response => {
              this.$modal.msgSuccess("处理成功!");
              this.open = false;
              this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单编号为"' + ids + '"的数据项？').then(function() {
        return delEmsWorkorder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/emsWorkorder/export', {
        ...this.queryParams
      }, `emsWorkorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
